﻿Public Class ProjectTypeManageForm

    Private Sub CompressorGroupManageForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDevTypeGroup()
    End Sub

    Private _pi As ProjectItem

    Private Sub LoadDevTypeGroup()

        _pi = MainForm.pi

        _isModified = False
        Dim bFirst As Boolean = True

        For Each dti As DevTypeItem In ProjectService.GetDevRefTypeList(_pi)
            AddGroupControl(dti)
        Next

        If Me.FlowLayoutPanel1.Controls.Count > 0 Then
            CType(Me.FlowLayoutPanel1.Controls(0), ProjectTypeControl).groupSelected = True
            CType(Me.FlowLayoutPanel1.Controls(0), ProjectTypeControl).Focus()
            Me.btnModifyGroup.Enabled = True
            Me.btnDeleteGroup.Enabled = True
        Else
            Me.btnModifyGroup.Enabled = False
            Me.btnDeleteGroup.Enabled = False
        End If

    End Sub

    Private Sub AddGroupControl(ByVal dti As DevTypeItem)
        Dim c As New ProjectTypeControl

        c.dtiItem = dti

        c.SetEnable(False)
        c.groupSelected = False
        


        AddHandler c.GroupSelectEvent, AddressOf CompressorGroupControl_GroupSelectEvent


        Me.FlowLayoutPanel1.Controls.Add(c)
    End Sub


    Private _isModified As Boolean
    Public Property IsModified() As Boolean
        Get
            Return _isModified
        End Get
        Set(ByVal value As Boolean)
            _isModified = value
        End Set                
    End Property


    Private Sub btnAddGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddGroup.Click
        Dim frm As New ProjectTypeForm(_pi.ProjectID)
        'frm.btn_del.Enabled = False
        'frm.btn_modify.Enabled = False

        Dim tip As String

        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then


            If ProjectService.AddDevRefTypeList(frm.dti, Me.FlowLayoutPanel1.Controls.Count) = True Then

                AddGroupControl(frm.dti)
                'Log.log(tbccLogOperateType.ModifyProject, String.Format("增加设备{0}", frm.dti.NetId))

                _isModified = True

                '刷新工程配置()
                MainForm.RefeshCurrentProjectInfo()
                msg.Show("设备添加成功")
            Else
                tip = String.Format("增加设备【{0}】失败", frm.dti.NetId)
                msg.ShowError(tip)
            End If
        End If

    End Sub

    Private Sub CompressorGroupControl_GroupSelectEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For Each c As ProjectTypeControl In Me.FlowLayoutPanel1.Controls
            c.groupSelected = False
            c.SetEnable(False)
        Next

        CType(sender, ProjectTypeControl).groupSelected = True
        Me.btnDeleteGroup.Enabled = True
        Me.btnModifyGroup.Enabled = True

    End Sub

    Private Function GetSelectedControl() As ProjectTypeControl
        Dim item As ProjectTypeControl = Nothing

        For Each c As ProjectTypeControl In Me.FlowLayoutPanel1.Controls
            If c.groupSelected Then
                item = c
                Exit For
            End If
        Next

        Return item
    End Function
    '******************************************************************
    '2009年0619 李吉鹏修改 为增加设备类型修改功能 
    Private Sub btnModifyGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModifyGroup.Click
        Dim ctl As ProjectTypeControl
        ' Dim tip As String

        ctl = GetSelectedControl()

        If ctl Is Nothing Then
            Exit Sub
        End If
        

        Dim frm As New ProjectTypeForm(ctl.dtiItem, _pi.ProjectID)


        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ctl.FlowLayoutPanel2.Controls.Clear()

            ctl.dtiItem = frm.dti

            
            msg.Show("修改成功")
            _isModified = True

            '刷新工程配置
            MainForm.RefeshCurrentProjectInfo()
        Else
            
        End If
    End Sub

    Private Sub btnDeleteGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteGroup.Click
        Dim ctl As ProjectTypeControl
        ctl = GetSelectedControl()

        If ctl Is Nothing Then
            Exit Sub
        End If

        '模式2允许任意删除设备
        Select Case MainForm.readConfig("SystemType")
            Case 2

            Case Else
                If Integer.Parse(Trim(ctl.tb_netid.Text)) <> ProjectService.DevTypeMaxNetid(_pi.ProjectID, ctl.dtiItem.DevAppType) Then
                    msg.Show("请按顺序从最大设备开始删除")
                    Exit Sub
                End If
        End Select

       


        Dim tip As String
        tip = String.Format("是否要删除设备【{0}】的所有配置信息？（删除前请确认已将配置对应端口清0！）", ctl.dtiItem.NetId)
        If msg.ShowYesNo(tip) = Windows.Forms.DialogResult.Yes Then
            If ProjectService.DelDevRefTypeList(ctl.dtiItem, Me.FlowLayoutPanel1.Controls.Count) = True Then

                Me.FlowLayoutPanel1.Controls.Remove(ctl)
                _isModified = True

                '刷新工程配置
                MainForm.RefeshCurrentProjectInfo()

                msg.Show("删除成功")
            Else
                tip = String.Format("删除设备【{0}】失败", ctl.dtiItem.NetId)
                msg.ShowError(tip)
            End If
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class